const db = require('../tool/db.js');
const common = require('../model/common.js')

async function adminSendGlobal(data){
    let [res] = await db.query('insert into message_text (admin_id,send_time,message_txt,type) values(?,?,?,?)',[data.adminID,data.time,data.message,0])
    if(res.insertId){
        return {code:1,msg:'发布成功',data:'' }
    }
}


async function adminSendSingle(data){
    let time = Date.now()
    let [res] = await db.query('insert into message_text (admin_id,send_time,message_txt,type) values(?,?,?,?)',[data.adminID,time,data.message,1])
    if(res.insertId){
        let [res2] = await db.query('insert into message (user_id,message_id) values(?,?)',[data.uid,res.insertId])
        if(res2.insertId){
            return {code:1,msg:'发送成功',data:''}
        }
    }
}


async function usermessageList(data){
    //查询用户所有站内信
    let [res] = await db.query('select * from message where user_id = ? order by id desc',[data])
    if(!res.length){
        res.push({message_id : 0})
    }

    //查询用户未接收到的站内信
    let [res2] = await db.query('select * from message_text where id > ? and type = 0',[res[0].message_id]) 
    if(res2.length){
        let values = ''
        for (const i of res2) {
            values += `(${data},${i.id}),`
        }
        
        let sql = 'insert into message (user_id,message_id) values'  + values 
        sql = sql.slice(0,sql.length-1)
        //接收站内信
        let [res3] = await db.query(sql)
        if(!res3.insertId){
            return {code:0,msg:'无最新消息',data:''}
        }
    }
        //查询最新站内信
    let [res4] = await db.query('select message_id,message_txt,admin_id,send_time from message_text as b join (select * from message where user_id = ? and delstatus = 1) as a on a.message_id = b.id',[data])

    return {code:1 ,msg:'查询成功',data:res4}
         
   

}

async function adminList(data){
    //1全局 2个人 3已删除
    let sql = ''
    switch (data.id) {
        case 1:
            sql = 'select * from message_text where type = 0 order by id desc'
            break;
        case 2:
            sql = 'select user_id as uid,a.id,message_txt,admin_id,send_time from message as b join (select * from message_text where type = 1) as a on a.id = b.message_id order by a.id desc'
            break;
        case 3:
            sql = 'select * from message_text where admin_status = 0'
            break;
        default: sql = 'select * from message_text order by id desc'
            break;
    }
    let [res] = await db.query(sql+ ' limit ?,?',[data.offset,data.num])

    if(res){
        if(res.length){
            return {code:1,msg:'成功',data:res}
        }else{
            return {code:0,msg:'暂无数据',data:''}
        }
    }else{
        return {code:0,msg:'错误',data:''}
    }
}

async function admindel(data){
    let [res] = await db.query('update message_text set admin_status = 0 where id = ' + data)
    if(res.affectedRows){
        return {code:1,msg:'删除成功',data:''}
    }

}

async function read(data){
    let [res] = await db.query('update message set status = 1 where message_id = ? and user_id = ?',[data.id,data.uid])
    if(res.affectedRows){
        return {code:1,msg:'成功',data:''}
    }
}

async function userdel(data){

    let [res] = await db.query('update message set delstatus = 0 where message_id = ? and user_id = ?',[data.id,data.uid])
    if(res.affectedRows){
        return {code:1,msg:'删除成功',data:''}
    }
}

async function reads(uid){
    let [res]  = await db.query('update message set status = 1 where user_id = ' + uid)
    if(res.affectedRows){
        return {code:1,msg:'修改成功',data:''}
    }
}
module.exports = {adminSendGlobal,adminSendSingle,usermessageList,adminList,admindel,read,userdel,reads}